package reflecct;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @classname:Fileds
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/23 20:22
 * @version: 0.1
 * @since:1.8
 */
public class Fileds {
    public static void main(String[] args) {
        demo1();
    }

    private static void demo1() {
        String name = "jim";
        String pass = "1234";
        String phone = "199201224445";

        Map<String,Object> map  = new HashMap<>(16);
        map.put("name",name);
        map.put("pass",pass);
        map.put("phone",phone);

        try {
            Class<?> aClass = Class.forName("reflecct.User");
           User user= (User) aClass.newInstance();

            map.forEach((key,value)->{
                try {
                    Field field = aClass.getDeclaredField(key);
                    field.setAccessible(true);
                    field.set(user,value);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            });

            System.out.println(user);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


    }
}
